package com.wkbb.sports.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wkbb.common.base.BaseServiceImpl;
import com.wkbb.common.dto.ResultDTO;
import com.wkbb.common.dto.basic.GradeDto;
import com.wkbb.common.dto.basic.OrganizationDto;
import com.wkbb.common.enums.OrgGradeTypeEnum;
import com.wkbb.common.enums.OrganizationTypeEnum;
import com.wkbb.common.vo.Tag;
import com.wkbb.sports.dto.OrgGradeDto;
import com.wkbb.sports.dto.SettingDto;
import com.wkbb.sports.feign.BasicFeignService;
import com.wkbb.sports.mapper.BasicMapper;
import com.wkbb.sports.mapper.HlifeBannerMapper;
import com.wkbb.sports.mapper.HlifeOrgGradeMapper;
import com.wkbb.sports.mapper.HlifeSportLevelMapper;
import com.wkbb.sports.model.HlifeBanner;
import com.wkbb.sports.model.HlifeOrgGrade;
import com.wkbb.sports.model.HlifeSportLevel;
import com.wkbb.sports.service.SettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SettingServiceImpl extends BaseServiceImpl implements SettingService {

    @Autowired
    private BasicFeignService basicFeignService;

    @Autowired
    private HlifeOrgGradeMapper hlifeOrgGradeMapper;

    @Autowired
    private HlifeBannerMapper hlifeBannerMapper;

    @Autowired
    private HlifeSportLevelMapper hlifeSportLevelMapper;
    @Autowired
    private BasicMapper basicMapper;

    @Override
    @Transactional(readOnly = true)
    public List<HlifeSportLevel> getSportLevelList(Map<String, Object> map) {
        return hlifeSportLevelMapper.getSportLevelList(map);
    }

    @Override
    public void putSportLevel(HlifeSportLevel data) {
        hlifeSportLevelMapper.update(data);
    }

    @Override
    public ResultDTO<Boolean> putTag(Tag info) {
        if (info.getRank() == null) {
            info.setRank(999);
        }
        if (info.getParentId() != null) {
            Tag parentLabel = basicMapper.getTagById(info.getParentId());
            if (parentLabel == null) {
                return ResultDTO.errorResult("添加失败，不存在的父类");
            }
            info.setLevel(parentLabel.getLevel() + 1);
        } else {
            info.setLevel(1);
        }
        if (info.getId() == null) {    //新增
            int insertLabel = basicMapper.insertTag(info);
            if (insertLabel > 0) {
                return new ResultDTO<>(true);
            }
        } else {          //修改
            int updateLabel = basicMapper.updateTag(info);
            if (updateLabel > 0) {
                return new ResultDTO<>(true);
            }
        }
        return new ResultDTO<>(false);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<Tag> getTagListByPage(Map<String, Object> params, int pageNo, int pageSize) {
        Page<Tag> page = PageHelper.startPage(pageNo, pageSize,true);
        PageInfo<Tag> pageInfo = new PageInfo<Tag>(basicMapper.getTagList(params));
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    @Override
    @Transactional(readOnly = true)
    public List<Tag> getTagList(Map<String, Object> params) {
        return basicMapper.getTagList(params);
    }

    @Override
    @Transactional(readOnly = true)
    public List<Tag> getCategoryList(Map<String, Object> params) {
        return basicMapper.getCategoryList(params);
    }

    @Override
    @Transactional(readOnly = true)
    public List<GradeDto> getOrgGradeList(Integer gradeType)
    {
        ResultDTO<OrganizationDto> organizationResult = basicFeignService.getOrgById(1L);
        List<GradeDto> gradeList = new ArrayList<>();
        if(organizationResult.isSuccess()&&null!=organizationResult.getData())
        {
//            OrganizationDto organizationDto = organizationResult.getData();
//            log.info("gradeType:{} ",gradeType);
            //查出十二年一贯学校年级，再根据学校类型转换
            ResultDTO<List<GradeDto>> getGradeInfoResult = basicFeignService.getGradeList(1L,null);
//            ResultDTO<List<GradeDto>> getGradeInfoResult = basicFeignService.getGradeInfoList(OrgGradeTypeEnum.TWELVE_CONSISTENCY_SCHOOL.getValue());
            List<GradeDto> gradeInfoList = getGradeInfoResult.getData();
            int max=55,min=40;
            if (gradeType!=null && gradeType == 1){
                max = 48;
            }
//            switch (gradeType){
//                case 6:
//                    max = 45;
//                    min = 40;
//                    break;
//                case 8:
//                    max = 48;
//                    min = 46;
//                    break;
//                case 9:
//                    max = 51;
//                    min = 49;
//                    break;
//                case 14:
//                    max = 48;
//                    min = 40;
//                    break;
//            }
            for(GradeDto gradeDto : gradeInfoList)
            {
                if(gradeDto.getId()>=min && gradeDto.getId()<=max)
                {
                    gradeList.add(gradeDto);
                }
            }
        }
        return gradeList;
    }

    @Override
    @Transactional(readOnly = true)
    public SettingDto getOrgSetting(Long orgId,Integer gradeType)
    {
        SettingDto settingDto = new SettingDto();

        List<OrgGradeDto> orgGradeList = new ArrayList<>();

        //查询学校年级信息
//        ResultDTO<OrganizationDto> organization = basicFeignService.getOrgById(orgId);
//
//        Integer gradeType1 = this.getGradeType(organization.getData().getOrgType());
        
//        ResultDTO<List<GradeDto>> getGradeResult = basicFeignService.getGradeList(orgId,gradeType1);
        ResultDTO<List<GradeDto>> getGradeResult = basicFeignService.getGradeList(null,gradeType);

        List<GradeDto> gradeList = getGradeResult.getData();

        //查询学校设置的信息
        Map<String, Object> gradeParams = new HashMap<String, Object>();
        gradeParams.put("orgId",orgId);
        List<HlifeOrgGrade> hlifeOrgGradeList = hlifeOrgGradeMapper.getListByMapParams(gradeParams);
        for(GradeDto gradeDto : gradeList)
        {
            boolean existFlag = false;
            for(HlifeOrgGrade hlifeOrgGrade : hlifeOrgGradeList) {
                if(gradeDto.getId().equals(hlifeOrgGrade.getGradeId()))
                {
                    OrgGradeDto orgGrade = new OrgGradeDto();
                    orgGrade.setGradeId(hlifeOrgGrade.getGradeId());
                    orgGrade.setGradeName(gradeDto.getGradeName());
                    orgGrade.setRuleGradeId(hlifeOrgGrade.getRuleGradeId());
                    orgGradeList.add(orgGrade);
                    existFlag = true;
                    break;
                }
            }

            if(!existFlag)
            {
                OrgGradeDto orgGrade = new OrgGradeDto();
                orgGrade.setGradeId(gradeDto.getId());
                orgGrade.setGradeName(gradeDto.getGradeName());
                orgGrade.setRuleGradeId(null);
                orgGradeList.add(orgGrade);
            }
        }

        settingDto.setOrgGradeList(orgGradeList);

        return settingDto;
    }

    @Override
    public void saveOrgSetting(SettingDto settingDto)
    {
        Long orgId = settingDto.getOrgId();

        //先删除再新增
        HlifeOrgGrade orgGrade = new HlifeOrgGrade();
        orgGrade.setOrgId(orgId);
        hlifeOrgGradeMapper.delete(orgGrade);

        //设置年级对应关系
        List<OrgGradeDto> orgGradeList = settingDto.getOrgGradeList();
        for(OrgGradeDto orgGradeDto : orgGradeList)
        {
            HlifeOrgGrade hlifeOrgGrade = new HlifeOrgGrade();
            hlifeOrgGrade.setOrgId(orgId);
            hlifeOrgGrade.setGradeId(orgGradeDto.getGradeId());
            hlifeOrgGrade.setRuleGradeId(orgGradeDto.getRuleGradeId());
            hlifeOrgGrade.setCreateUser(settingDto.getCreateUser());
            hlifeOrgGrade.setUpdateUser(settingDto.getUpdateUser());

            hlifeOrgGradeMapper.insert(hlifeOrgGrade);
        }
    }

    @Override
    public void putBanner(HlifeBanner hlifeBanner) throws Exception {
        if (hlifeBanner.getId() == null){
            hlifeBannerMapper.insert(hlifeBanner);
        }else{
            hlifeBannerMapper.update(hlifeBanner);
        }
    }

    @Override
    public void delBanner(Long id) {
        hlifeBannerMapper.deleteByPrimaryKey(id);
    }

    @Override
    @Transactional(readOnly = true)
    public PageInfo<HlifeBanner> getBannerListByPage(Map<String, Object> params, int pageNo, int pageSize) {
        Page<HlifeBanner> page = PageHelper.startPage(pageNo, pageSize,true);
        PageInfo<HlifeBanner> pageInfo = new PageInfo<HlifeBanner>(hlifeBannerMapper.getListByPage(params));
        pageInfo.setTotal(page.getTotal());
        return pageInfo;
    }

    private Integer getGradeType(Integer orgType) {
        int gradeType = 10;
        if (orgType == OrganizationTypeEnum.KINDERGARTEN.getValue().intValue()) {
            gradeType = 1;
        } else if (orgType == OrganizationTypeEnum.PRIMARY_SCHOOL.getValue().intValue()) {
            gradeType = 2;
        //} else if (orgType == OrganizationTypeEnum.SECONDARY_SCHOOL.getValue().intValue()) {
          //  gradeType = 3;
        } else if (orgType == OrganizationTypeEnum.MIDDLE_SCHOOL.getValue().intValue()) {
            gradeType = 4;
        } else if (orgType == OrganizationTypeEnum.HIGH_SCHOOL.getValue().intValue()) {
            gradeType = 5;
       // } else if (orgType == OrganizationTypeEnum.VOCATIONAL_SCHOOL.getValue().intValue()) {
           // gradeType = 6;
        //} else if (orgType == OrganizationTypeEnum.UNIVERSITY.getValue().intValue()) {
         //   gradeType = 7;
       // } else if (orgType == OrganizationTypeEnum.SIX_CONSISTENCY_SCHOOL.getValue().intValue()) {
           // gradeType = 8;
        } else if (orgType == OrganizationTypeEnum.NINE_CONSISTENCY_SCHOOL.getValue().intValue()) {
            gradeType = 9;
        } else if (orgType == OrganizationTypeEnum.TWELVE_CONSISTENCY_SCHOOL.getValue().intValue()) {
            gradeType = 10;
        }

        return gradeType;
    }
}
